<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Adminmod extends CI_Model 
{

    function __construct()
    {
        parent::__construct();
    	$this->load->database();

	}
	
//################## commun function################################
	public function updatetemplate($tablename,$data,$where)
		{
				
			$str = $this->db->update_string($tablename, $data, $where);
			
			$query = $this->db->query($str);
			//print_r($query);
			
		}	
		
	public function insertetemplate($tablename,$data)
		{
				
			$str = $this->db->insert_string($tablename, $data);
			 
			$query = $this->db->query($str);
			$idinsert=$this->db->insert_id();
			return $idinsert;
			
		}	

		
		
//######################### end commun function###########################

		
		public function getlistdept()
		{
			$query=$this->db->query("SELECT * from department");
			return $query->result_array();
		}
		
		public function getlistcats()
		{
			$query=$this->db->query("SELECT * from category");
			return $query->result_array();
		}
		
		public function getlistproduct()
		{
			$query=$this->db->query("SELECT  p.idproduct, p.productname_en, p.productname_fr, p.description_en,p.description_fr,
sildershow,p.video,fk_idcategory ,catname_en FROM product as p 
inner join category as c on c.idcategory=p.fk_idcategory order by c.idcategory ;");
			return $query->result_array();
		}
		
		public function getlistlocaux()
		{
			$query=$this->db->query("SELECT * from restolocal");
			return $query->result_array();
		}
		
		
	//---------------------------------	
		
		
		public function getlistdeptinfo($iddept)
		{
			$query=$this->db->query("SELECT * from department where iddepartment=$iddept");
			return $query->result_array();
		}

		public function getlistcatinfo($idcat)
		{
			$query=$this->db->query("SELECT * from category where idcategory=$idcat");
			return $query->result_array();
		}
		
		public function getlistporductinfo($idproduct)
		{
			$query=$this->db->query("SELECT * from product where idproduct=$idproduct");
			return $query->result_array();
		}
		
		public function getlistlocauxinfo($idlocal)
		{
			$query=$this->db->query("SELECT * from restolocal where idlocal=$idlocal");
			return $query->result_array();
		}
		
	public function getlistlocaldept($idlocal)
		{
			$query=$this->db->query("select idlocaldepartment, ld.description_en ,ld.descriptions_fr, 
			ld.order,ld.fk_local_id,ld.fk_depart_id,d.deptname_en,d.deptname_fr 
			from localdepartment as ld inner join department as d  on d.iddepartment=ld.fk_depart_id where fk_local_id=$idlocal");
			return $query->result_array();
		}
		

	public function getlistdeptnoselected($idlocal)
		{
			$query=$this->db->query("select iddepartment, deptname_en,deptname_fr  from department where iddepartment not in(select  fk_depart_id from localdepartment where fk_local_id=$idlocal)");
			return $query->result_array();
		}	
		
	public function getlistperiod()
	{
		$query=$this->db->query("SELECT * FROM period;");
		return $query->result_array();
	}
	
	public function getlistcatsforlocalcat($idlocal)
	{
		$query=$this->db->query("select c.idcategory,c.catname_en,c.catname_fr,c.description_en,c.description_fr,c.image,c.video, d.deptname_en,d.deptname_fr,
								ld.description_en as localdeptdesc_en,ld.descriptions_fr as localdeptdesc_fr,c.idcategory,ld.idlocaldepartment,d.iddepartment 
								from category as c 
								inner join department as d on c.fk_iddepartment=d.iddepartment 
								inner join localdepartment as ld on ld.fk_depart_id= d.iddepartment 
								 where ld.fk_local_id=$idlocal");
		return $query->result_array(); 
	}

	
	public  function getlocalcatexit($idlocal)
	{
		$query=$this->db->query("select c.idcategory,c.catname_en,c.catname_fr,c.description_en,c.description_fr,c.image,c.video
								,lc.description_en as loacalcatdesc_en, lc.description_fr as loacalcatdesc_fr, lc.order as localcatorder,p.idperiod,p.name_en as periodname
								,lc.idlocalcats
								from localcat as lc
					            inner join category as c on c.idcategory=lc.fk_idcategory
					           	inner join period as p on p.idperiod=lc.period_idperiod
					            where lc.fk_local_id=$idlocal order by periodname");
	
	return $query->result_array(); 
	}
	
	function getlistlocalcatbylocal($idlocal)
		{
		$query=$this->db->query("select c.catname_en,lc.fk_idcategory ,idlocalcats,p.name_en as periodname from localcat  as lc 
								inner join category   as c on   lc.fk_idcategory=c.idcategory
								inner join period as p on p.idperiod=lc.period_idperiod
								where fk_local_id=$idlocal");
		return $query->result_array(); 
		
		}

		
	function getlistproductnoselectbylcat($idlocalcat)
		{
			$query=$this->db->query("select p.idproduct, p.productname_en,p.productname_fr,p.productname_en,p.productname_fr ,p.fk_idcategory from product as p inner join category  as  c on c.idcategory=p.fk_idcategory   
										where c.idcategory in (select fk_idcategory from localcat where idlocalcats=$idlocalcat) 
										and p.idproduct not in(select idproduct from localproduct where localcat_idlocalcats=$idlocalcat)") ;
			
			return $query->result_array();
		}

		
	function getlistproductselection($idlocalcat)
		{
			$query=$this->db->query("SELECT p.productname_en,p.idproduct, lp.description_en,lp.description_fr,lp.order,lp.fk_idcategory ,lp.idlocalproduct FROM localproduct  as lp inner join  product as p on p.idproduct=lp.idproduct where localcat_idlocalcats=$idlocalcat" ) ;
			
			return $query->result_array();
		}		
			
	
	
}
